#!/usr/bin/env python
import platform, os, sys

Import('env')
Import('env_modules')
env_module = env_modules.Clone()
JS_ENGINE = 'quickjs'

def open_file(path, mode):
	if platform.python_version() > '3':
		return open(path, mode, encoding='utf8')
	else:
		return open(path, mode)

def dump_text_file_to_cpp(file):
	source = open_file(file, 'r').read()
	lines = source.split('\n')
	source = ""
	length = len(lines)
	for i in range(length):
		line = lines[i].replace('"', '\\"')
		line = '\t"' + line + '\\n"'
		if i < length -1:
			line += "\n"
		source += line
	return source

if JS_ENGINE == 'quickjs':
	# generate builtin binding code
	import generate_builtin_api
	generate_builtin_api.generate_api_json(os.path.join(GetLaunchDir(), "modules", os.path.basename(os.getcwd())))
	import quickjs.builtin_binding_generator
	# quickjs.builtin_binding_generator.generate_builtin_bindings()
	# build quickjs source
	version = open('quickjs/quickjs/VERSION', 'r').read().split('\n')[0]
	env_module.Append(CPPDEFINES={"QUICKJS_CONFIG_VERSION": '"'+ version +'"'})
	env_module.Append(CPPDEFINES=["CONFIG_BIGNUM"])
	if env_module['target'] != 'release':
		env_module.Append(CPPDEFINES={"DUMP_LEAKS": 1})
		env_module.Append(CPPDEFINES={"QUICKJS_WITH_DEBUGGER": 1})
	env_module.Append(CPPPATH=["quickjs/quickjs"])
	env_module.add_source_files(env.modules_sources, 'quickjs/*.cpp')
	env_module.add_source_files(env.modules_sources, 'quickjs/quickjs/*.c')

# Binding script to run at engine initializing
with open("misc/godot.binding_script.gen.cpp", "w") as f:
	text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "../ecmascript_binder.h"\nString ECMAScriptBinder::BINDING_SCRIPT_CONTENT = \n${source};'
	f.write(text.replace('${source}', dump_text_file_to_cpp("misc/binding_script.js")))

# gen.cpp
with open_file("tools/godot.d.ts.gen.cpp", "w") as f:
	text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::BUILTIN_DECLARATION_TEXT = \n${source};'
	f.write(text.replace('${source}', dump_text_file_to_cpp("misc/godot.d.ts")))
with open_file("tools/tsconfig.json.gen.cpp", "w") as f:
	text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::TSCONFIG_CONTENT = \n${source};'
	f.write(text.replace('${source}', dump_text_file_to_cpp("misc/tsconfig.json")))
with open_file("tools/decorators.ts.gen.cpp", "w") as f:
	text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::TS_DECORATORS_CONTENT = \n${source};'
	f.write(text.replace('${source}', dump_text_file_to_cpp("misc/decorators.ts")))
with open_file("tools/package.json.gen.cpp", "w") as f:
	text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::PACKAGE_JSON_CONTENT = \n${source};'
	f.write(text.replace('${source}', dump_text_file_to_cpp("misc/package.json")))

sources = [
	'register_types.cpp',
	'ecmascript_language.cpp',
	'ecmascript_instance.cpp',
	'ecmascript.cpp',
	'misc/godot.binding_script.gen.cpp',
	'quickjs/quickjs_builtin_binder.gen.cpp',
	'tools/decorators.ts.gen.cpp',
	'tools/godot.d.ts.gen.cpp',
	'tools/package.json.gen.cpp',
	'tools/tsconfig.json.gen.cpp',
]

if env['tools']:
	with open_file("tools/godot.d.ts.gen.cpp", "w") as f:
		text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::BUILTIN_DECLARATION_TEXT = \n${source};'
		f.write(text.replace('${source}', dump_text_file_to_cpp("misc/godot.d.ts")))
	with open_file("tools/tsconfig.json.gen.cpp", "w") as f:
		text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::TSCONFIG_CONTENT = \n${source};'
		f.write(text.replace('${source}', dump_text_file_to_cpp("misc/tsconfig.json")))
	with open_file("tools/decorators.ts.gen.cpp", "w") as f:
		text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::TS_DECORATORS_CONTENT = \n${source};'
		f.write(text.replace('${source}', dump_text_file_to_cpp("misc/decorators.ts")))
	with open_file("tools/package.json.gen.cpp", "w") as f:
		text = '/* THIS FILE IS GENERATED DO NOT EDIT */\n#include "editor_tools.h"\nString ECMAScriptPlugin::PACKAGE_JSON_CONTENT = \n${source};'
		f.write(text.replace('${source}', dump_text_file_to_cpp("misc/package.json")))
	env_module.add_source_files(env.modules_sources, 'tools/*.cpp')

env_module.Append(CPPPATH=["#modules/ECMAScript"])
env_module.add_source_files(env.modules_sources, sources)
